package gipf.trunk;

import java.awt.GridLayout;
import java.awt.Point;
import java.awt.TextArea;
import java.awt.event.ComponentEvent;
import java.awt.event.ComponentListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.util.ArrayList;
import java.util.logging.Level;
import java.util.logging.Logger;

import javax.swing.JFrame;

@SuppressWarnings("serial")
public class BoardView extends JFrame implements ComponentListener {

	public static int width;
	public static int height;
	final static long tickInterval = 300;														;
	static TextArea ta1;
	static GipfPanel panel;
	public static BoardComponent comp;
	private static action nextAction = new action();

	public static void main(String args[]) {
		// Set-up
		final int argP;
		argP = 0; // int number of humans

		final GameEngine gameEngine = new GameEngine(argP, "BASIC");
		staticBoard adj = new staticBoard();

		
//		final Action action = new Action();

		// Make a frame
		final JFrame frame = new JFrame();

		frame.setSize(700, 700);
		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		frame.setVisible(true);
		// Add the panel
		BoardView.panel = new GipfPanel();
		frame.add(BoardView.panel);
		// Add the create components and add them to the panel
		comp = new BoardComponent();

//
//		ta1 = new TextArea();
//		BoardView.panel.add(ta1);
		BoardView.panel.add(comp);

		
//		ta1.setText("Scores and stuff.");
		// BoardView.panel.add(ta1);
		// Set layout
		BoardView.panel.setLayout(new GridLayout(2, 1));
		// Add the Event Listener
		// panel.addComponentListener(l);

		
		BoardView.panel.addMouseListener(new MouseListener() {

			@Override
			public void mouseExited(MouseEvent arg1) {
				// throw new
				// UnsupportedOperationException("Not supported yet.");

			};

			@Override
			public void mouseClicked(MouseEvent e) {
				if (e.getClickCount() % 2 == 0){
					position nextPosition = getPosition(e.getPoint());
					
					if(nextPosition != null){
						nextAction.setTakeIndex(nextPosition.getIndex());
					}
				}
					
			}

			@Override
			public void mousePressed(MouseEvent e) {
				// when only pressing once
				// add input for movementcommand
				position nextPosition = getPosition(e.getPoint());
				
				if(nextPosition != null && staticBoard.outter(nextPosition.getIndex())){
					nextAction.setPlace(nextPosition);
				}

				//
				//BoardView.panel.repaint();
				
				// throw new
				// UnsupportedOperationException("Not supported yet.");
			}

			private position getPosition(Point point) {
				for (position p : staticBoard.getPositionList()){
					int deltaX = p.getGUIcoordinate()[0] - point.x;
					int deltaY = p.getGUIcoordinate()[1] - point.y;
					if(deltaX < 30 && deltaY < 30 && deltaX > -30 && deltaY > -30)
						return p;
				}
				return null;
			}

			@Override
			public void mouseReleased(MouseEvent e) {
				// add input for movementcommand
				// action.receive(new MovementCommand().setVector(e.getPoint(),
//				 null), e.getWhen());
				position nextPosition = getPosition(e.getPoint());
				
				if(nextPosition != null && staticBoard.inner(nextPosition.getIndex())){
					nextAction.setMove(nextPosition);
				}
				
			}

			@Override
			public void mouseEntered(MouseEvent e) {
				// throw new
				// UnsupportedOperationException("Not supported yet.");
			}

		});

		panel.addComponentListener(new ComponentListener() {

			@SuppressWarnings("unused")
			public void componentClicked(ComponentEvent arg0) {
			}

			public void componentResized(ComponentEvent arg0) {
				BoardView.width = frame.getWidth();
				BoardView.height = frame.getHeight();
				BoardView.panel.repaint();
			}

			public void componentMoved(ComponentEvent arg0) {
			}

			public void componentShown(ComponentEvent arg0) {
			}

			public void componentHidden(ComponentEvent arg0) {
			}
		});
		
		try {
			gameEngine.tick(tickInterval);

		} catch (Exception ex) {
			Logger.getLogger(BoardView.class.getName()).log(Level.SEVERE, null,
					ex);
		}
		
		ArrayList<int[]> states = gameEngine.getStates();
		
		for(int i = 0; i < states.size(); i++){
			for (int z = 0; z < states.get(i).length; z++){
				System.out.print(states.get(i)[z]);
			}
			System.out.println();
		}

	}

	@Override
	public void componentHidden(ComponentEvent e) {
		// TODO Auto-generated method stub

	}

	@Override
	public void componentMoved(ComponentEvent e) {
		// TODO Auto-generated method stub

	}

	@Override
	public void componentResized(ComponentEvent e) {
		// TODO Auto-generated method stub

	}

	@Override
	public void componentShown(ComponentEvent e) {
		// TODO Auto-generated method stub

	}

	public static action getAction() {
		return nextAction;
	}
	
	public static void resetAction(){
		nextAction = new action();
	}

}